/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FileComponent;

import Bean.File.FileBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author DIT-0958372
 */
public class displayServlet extends HttpServlet {

    /** 
    19/04/2011
    1. The retrieving logic for file component is done, should be able to further
     *      develop to search function

    2. The display tag is now optimize for only displaying one item, which has a fixed index of 1.

    3. The same servlet could be used in the search function as well as other functions possible.

    4. Able to display files on the page
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        FileBean f = null;
        FileLogic fl = null;
        String msg = "";

        try {
            String condition = request.getParameter("condition");
            String index = request.getParameter("index");

            // The conditional statement is to prevent any unexpected situtation that
            //makes the condition empty
            if (!condition.equals("")) {
                fl = new FileLogic();
                if (index.equals("")) {
                    condition = "null";
                }
                // call the method in FileLogic to retrieve the file from databse
                f = fl.retrieveFileInfo(condition, index);
                session.setAttribute("fileInfo", f);
            } else {
                msg = "Programm Error! Condition not specified";
            }

            RequestDispatcher rd = request.getRequestDispatcher("FileComponent/uploadDisplay.jsp");
            rd.forward(request, response);

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
